package handler

import (
	db "echo_prj/db"
	"fmt"
	"github.com/labstack/echo"
	"log"
	"net/http"
	"time"
)


//登录
type cookieResult struct{
	Name string
	Value string
}
func Login(c echo.Context) error {
	var a = Res{}
	userName := c.FormValue("userName")
	passWord := c.FormValue("passWord")
	id,err := db.Login(userName,passWord)
	
	cookieArr := []cookieResult{
		{
			Name:"token",
			Value:encypt(fmt.Sprint(id)),
		},
		{
			Name:"id",
			Value:fmt.Sprint(id),
		},
	}
	term := time.Now().Add(24 * time.Hour)
	for _,v := range cookieArr{
		cookie := new(http.Cookie)
		cookie.Name = v.Name
		cookie.Value  = v.Value
		cookie.Expires = term
		c.SetCookie(cookie)
	}


	if err != nil{
		log.Println("Login,err=",err)
		a.Code=-1
		a.Msg=err.Error()
	}else{
		a = Res{
			Code:200,
			Msg:"",
			Result:id,
			}
	}
	return c.JSON(200,a)
}

func CheckLogin(c echo.Context) error {
	var a = Res{}
	a = Res{
		Code:200,
		Msg:"",
	}

	return c.JSON(200,a)
}